﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLSVPT1h
{
    public partial class Bang_diem_mon : Form
    {
        public Bang_diem_mon()
        {
            InitializeComponent();
        }

       

        private void Bang_diem_mon_Load(object sender, EventArgs e)
        {
            string connectionString = "Data Source=PC-200908251039;Initial Catalog=QLHS;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            string commandText = "select MaHS from HOCSINH";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(commandText, connectionString);
            DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet, "HOCSINH");
            DataTable dataTable = dataSet.Tables[0];

            foreach (DataRow dataRow in dataTable.Rows)
            {
                listBox1.Items.Add(dataRow["MaHS"]);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=PC-200908251039;Initial Catalog=QLHS;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                conn.Open();
                string commandText = "insert into BANGDIEM " +
                                     "values (@MaHS, @MaNH, @HocKy, @MaMH, @Diem15, @Diem1T, @DiemThi, @DiemTrungBinh)";

                SqlCommand comm = new SqlCommand(commandText, conn);

                string maHS = listBox1.SelectedItem.ToString();
                string maNH = comboBox1.SelectedItem.ToString();
                string hocKy = comboBox2.SelectedItem.ToString();
                string maMH = comboBox3.SelectedItem.ToString().Substring(0, 2);
                double diem15 = Convert.ToDouble(textBox1.Text);
                double diem1t = Convert.ToDouble(textBox2.Text);
                double diemThi = Convert.ToDouble(textBox3.Text);
                double diemTrungBinh = (diem15 + diem1t * 2 + diemThi * 3) / 6;

                comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
                comm.Parameters["@maHS"].Value = maHS;

                comm.Parameters.Add("@maNH", SqlDbType.VarChar, 6);
                comm.Parameters["@maNH"].Value = maNH;

                comm.Parameters.Add("@HocKy", SqlDbType.Char, 1);
                comm.Parameters["@HocKy"].Value = hocKy;

                comm.Parameters.Add("@maMH", SqlDbType.VarChar, 2);
                comm.Parameters["@MaMH"].Value = maMH;

                comm.Parameters.Add("@Diem15", SqlDbType.Float);
                comm.Parameters["@Diem15"].Value = diem15;

                comm.Parameters.Add("@Diem1t", SqlDbType.Float);
                comm.Parameters["@Diem1t"].Value = diem1t;

                comm.Parameters.Add("@DiemThi", SqlDbType.Float);
                comm.Parameters["@DiemThi"].Value = diemThi;

                comm.Parameters.Add("@DiemTrungBinh", SqlDbType.Float);
                comm.Parameters["@DiemTrungBinh"].Value = diemTrungBinh;

                comm.ExecuteNonQuery();

                MessageBox.Show("Dữ liệu đã được insert thành công");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
           

        }

        private void Bang_diem_mon_FormClosed(object sender, FormClosedEventArgs e)
        {
            Homepage_Form hp = new Homepage_Form();
            this.Visible = false;
            hp.Show();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Homepage_Form hp = new Homepage_Form();
            this.Visible = false;
            hp.Show();
        }
    }
}
